package com.example.bgmachine.bgmachine.popinputs;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

import com.example.bgmachine.bgmachine.R;
import com.example.bgmachine.bgmachine.goods.HomepageGridRightMenuAdapter;
import com.example.bgmachine.bgmachine.goods.HomepageGridRightMenuItemData;
import com.example.bgmachine.bgmachine.handlouts.HandlContentAdapter;
import com.example.bgmachine.bgmachine.handlouts.HandlContentData;
import com.example.bgmachine.bgmachine.homes.HomePages;
import com.example.bgmachine.bgmachine.specifs.SpecifData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class PopinputLists extends Dialog implements
        View.OnClickListener {
    Context context;

    private Boolean bool;
    private View.OnClickListener btnOnClick;

    private TextView spcwcif_popup_name_textview = null;

    private Button spcwcif_popup_close_btn = null;
    private Button pop_input_view_select_dialog_make_btn = null;

    public SpecifData m_typea = null;
    public String m_names = "";
    public String m_typeOrder = "";
    public String m_typeinfor = "";

    private GridView order_infor_item_gridvew = null;
    public PopinputAdapter mPopinputAdapter = null;
    public List<PopInputData> mPopInputData = null;

    public void setBtnOnClick(View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public void setBtnOkClick(String text,
                              View.OnClickListener btnOnClick) {
        this.btnOnClick = btnOnClick;

    }

    public PopinputLists(Context context) {
        super(context, R.style.SundayDialogs);
        this.context = context;
        bool = true;


        Log.d("aaa", "aaa===2");
        //create();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popinput_list_view_select_dialog);

        hideBottomUIMenu();
        // 设置窗口属性
        if (getWindow() != null) {
            getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            getWindow().setDimAmount(0f);
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
            );
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
                    WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
            );
        }

        init();
        //  setCancel(bool, "");

        Log.d("aaa", "aaa===1");
    }

    void init() {

        spcwcif_popup_name_textview = findViewById(R.id.spcwcif_popup_name_textview);

        spcwcif_popup_close_btn = findViewById(R.id.spcwcif_popup_close_btn);
        spcwcif_popup_close_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                dismiss();
            }
        });
        pop_input_view_select_dialog_make_btn = findViewById(R.id.pop_input_view_select_dialog_make_btn);
        pop_input_view_select_dialog_make_btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                dismiss();

                JSONArray arrys = new JSONArray();
                for (int i = 0; i < mPopInputData.size(); i++) {
                    PopInputData popInputData = mPopInputData.get(i);
                    arrys.put(popInputData.getMenuOutJson());
                }
                Log.d("aaa", "arrysarrysarrys=" + arrys);
                m_typeinfor = arrys.toString();
                HomePages.mHomePages.setMenusProduceProgress(m_typea,
                        m_names,
                        m_typeOrder,
                        m_typeinfor);
            }
        });

        order_infor_item_gridvew = findViewById(R.id.order_infor_item_gridvew);
        mPopInputData = new LinkedList<PopInputData>();
        mPopinputAdapter = new PopinputAdapter(context, (LinkedList<PopInputData>) mPopInputData);
        if (order_infor_item_gridvew != null) {
            order_infor_item_gridvew.setAdapter(mPopinputAdapter);
        }


    }

    /**
     * 隐藏虚拟按键，并且全屏
     */
    protected void hideBottomUIMenu() {
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getWindow().setAttributes(params);


        int flag = WindowManager.LayoutParams.FLAG_FULLSCREEN;
        Window myWindow = this.getWindow();
        myWindow.setFlags(flag, flag);

        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);


        }

        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
            }
        });
    }


    public void setNameInfor(String infors) {
        if (spcwcif_popup_name_textview != null) {
            spcwcif_popup_name_textview.setText(infors);
        }


    }

    public void setListInfor(String infors) {
        mPopInputData.clear();
        try {
            JSONArray arrys = new JSONArray(infors);
            for (int i = 0; i < arrys.length(); i++) {
                JSONObject atm = arrys.getJSONObject(i);
                Log.d("aaa", "setListInfor:atm" + atm);
                String materialpines = atm.optString("materialpines");
                String setbeverageOutput = atm.optString("setbeverageOutput");

                String names = HomePages.mHomePages.mPinsNames.get(Integer.parseInt(materialpines));
//                int typeid = atm.optInt("cupTypeid");
//                String typename = atm.optString("cupTypeNames");
//                String typeinfor = atm.optString("cupTypeReserve");
//                String cupTypeReserve2 = atm.optString("cupTypeReserve2");
//                String productCode = atm.optString("productCode");
//                String errorData="0";
//                if(atm.has("errorData")){
//                    errorData= atm.optString("errorData");
//                }
//                String price = atm.optString("price");
//                JSONArray content = atm.optJSONArray("children");
                mPopInputData.add(new PopInputData(Integer.parseInt(materialpines), names, setbeverageOutput, atm));

            }
        } catch (JSONException e) {
            e.printStackTrace();

        }


        if (mPopinputAdapter != null) {
            mPopinputAdapter.notifyDataSetChanged();
        }
    }

    /*
     *
     */
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        if (isShowing()) {
            //   dismiss();
        }
    }


}
